<template>
  <div class="service-records">
    <div class="timeline">
      <div v-for="(record, index) in serviceRecords" :key="index" class="timeline-item">
        <!-- 时间轴左侧 -->
        <div class="timeline-left">
          <div class="time-info">{{ record.date }}</div>
          <div class="status-badge" :class="record.statusClass">
            <span class="status-text">{{ record.statusText }}</span>
          </div>
        </div>

        <!-- 时间轴线 -->
        <div class="timeline-line">
          <div class="timeline-dot" :class="record.statusClass"></div>
          <div v-if="index < serviceRecords.length - 1" class="timeline-connector"></div>
        </div>

        <!-- 时间轴右侧内容 -->
        <div class="timeline-right">
          <div class="service-card">
            <div class="service-header">
              <h4 class="service-title">服务信息</h4>
              <el-button type="text" class="edit-link">编辑</el-button>
            </div>

            <el-row :gutter="20" class="service-details">
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务编号</span>
                  <span class="detail-value">{{ record.serviceNumber }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务状态</span>
                  <el-tag :type="record.statusTagType" size="small">{{ record.serviceStatus }}</el-tag>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务机构</span>
                  <span class="detail-value">{{ record.institution }}</span>
                </div>
              </el-col>

              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务团队</span>
                  <span class="detail-value">{{ record.team }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务医生</span>
                  <span class="detail-value">{{ record.doctor }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务包</span>
                  <span class="detail-value">{{ record.servicePackage }}</span>
                </div>
              </el-col>

              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务项目</span>
                  <span class="detail-value">{{ record.serviceItem }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务地点</span>
                  <span class="detail-value">{{ record.location }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务来源</span>
                  <span class="detail-value">{{ record.source }}</span>
                </div>
              </el-col>

              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">提交时间</span>
                  <span class="detail-value">{{ record.submitTime }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">审核时间</span>
                  <span class="detail-value">{{ record.reviewTime }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">审核人</span>
                  <span class="detail-value">{{ record.reviewer }}</span>
                </div>
              </el-col>

              <el-col :span="8">
                <div class="detail-item">
                  <span class="detail-label">服务时间</span>
                  <span class="detail-value">{{ record.serviceTime }}</span>
                </div>
              </el-col>
              <el-col :span="16">
                <div class="detail-item">
                  <span class="detail-label">服务备注</span>
                  <span class="detail-value">{{ record.note }}</span>
                </div>
              </el-col>
            </el-row>

            <!-- 健康随访信息 (仅已完成状态显示) -->
            <div v-if="record.statusClass === 'completed' && record.healthInfo" class="health-info">
              <h5 class="health-title">健康随访信息</h5>
              <el-row :gutter="20">
                <el-col :span="8">
                  <div class="detail-item">
                    <span class="detail-label">血压</span>
                    <span class="detail-value">{{ record.healthInfo.bloodPressure }}</span>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="detail-item">
                    <span class="detail-label">空腹血糖</span>
                    <span class="detail-value">{{ record.healthInfo.bloodSugar }}</span>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="detail-item">
                    <span class="detail-label">心率</span>
                    <span class="detail-value">{{ record.healthInfo.heartRate }}</span>
                  </div>
                </el-col>
                <el-col :span="24">
                  <div class="detail-item">
                    <span class="detail-label">疾病描述</span>
                    <span class="detail-value">{{ record.healthInfo.description }}</span>
                  </div>
                </el-col>
                <el-col :span="24">
                  <div class="detail-item">
                    <span class="detail-label">健康指导</span>
                    <span class="detail-value">{{ record.healthInfo.guidance }}</span>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部添加服务按钮 -->
    <div class="add-service">
      <el-button type="primary" @click="addService">添加服务</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// Props
const props = defineProps({
  residentId: {
    type: [String, Number],
    required: true
  }
})

// 响应式数据
const serviceRecords = ref([
  {
    date: '2021/10/09 10:09:09',
    statusClass: 'pending',
    statusText: '待服务',
    serviceNumber: '2021340001',
    serviceStatus: '待服务',
    statusTagType: 'warning',
    institution: '洛西社区卫生服务站',
    team: '李明明团队',
    doctor: '李明',
    servicePackage: '老年人服务包',
    serviceItem: '高血压随访（例行随访）',
    location: '机构门诊',
    source: '居民申请',
    submitTime: '2020/10/09 10:09:09',
    reviewTime: '2020/10/09 10:09:09',
    reviewer: '王敏',
    serviceTime: '2020/10/09 10:09:09',
    note: '上次的药已经吃完了'
  },
  {
    date: '2020/10/09 10:09:09',
    statusClass: 'completed',
    statusText: '已完成',
    serviceNumber: '2021340001',
    serviceStatus: '已完成',
    statusTagType: 'success',
    institution: '洛西社区卫生服务站',
    team: '李明明团队',
    doctor: '李明',
    servicePackage: '老年人服务包',
    serviceItem: '高血压随访（例行随访）',
    location: '机构门诊',
    source: '居民申请',
    submitTime: '2020/10/09 10:09:09',
    reviewTime: '2020/10/09 10:09:09',
    reviewer: '王敏',
    serviceTime: '2020/10/09 10:09:09',
    note: '上次的药已经吃完了',
    healthInfo: {
      bloodPressure: '120/80mmHg',
      bloodSugar: '90mmol/L',
      heartRate: '80bmp',
      description: '头晕头痛；恶心干呕',
      guidance: '饮食要清淡；适度运动'
    }
  }
])

// 方法
const loadServiceRecords = async () => {
  try {
    // 加载服务记录数据
    // const response = await axios.get(`/service/getServiceRecords?residentId=${props.residentId}`)
    // serviceRecords.value = response.data
    console.log('加载服务记录，居民ID:', props.residentId)
  } catch (error) {
    console.error('加载服务记录失败:', error)
    ElMessage.error('加载服务记录失败')
  }
}

const addService = () => {
  console.log('添加服务')
}

// 组件挂载时执行
onMounted(() => {
  loadServiceRecords()
})
</script>

<style scoped>
.service-records {
  background: transparent;
  padding: 0;
}

.timeline {
  position: relative;
  padding: 20px 0;
}

.timeline-item {
  display: flex;
  margin-bottom: 30px;
  position: relative;
}

.timeline-left {
  width: 140px;
  padding-right: 20px;
  text-align: right;
  flex-shrink: 0;
}

.time-info {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.status-badge.pending {
  background: #FDF6EC;
  color: #E6A23C;
}

.status-badge.completed {
  background: #F0F9FF;
  color: #67C23A;
}

.status-text {
  font-weight: 500;
}

.timeline-line {
  width: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  flex-shrink: 0;
}

.timeline-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  position: relative;
  z-index: 2;
}

.timeline-dot.pending {
  background: #E6A23C;
}

.timeline-dot.completed {
  background: #67C23A;
}

.timeline-connector {
  width: 2px;
  flex: 1;
  background: #DCDFE6;
  margin-top: 8px;
}

.timeline-right {
  flex: 1;
  padding-left: 20px;
}

.service-card {
  background: #f8f9fb;
  border-radius: 8px;
  padding: 20px;
  border: 1px solid #EBEEF5;
}

.service-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.service-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin: 0;
}

.edit-link {
  color: #409EFF;
  padding: 0;
}

.service-details {
  margin: 0;
}

.detail-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  min-height: 24px;
}

.detail-label {
  display: inline-block;
  width: 80px;
  font-size: 14px;
  color: #606266;
  margin-right: 10px;
  flex-shrink: 0;
}

.detail-value {
  font-size: 14px;
  color: #303133;
  word-break: break-all;
}

.health-info {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #EBEEF5;
}

.health-title {
  font-size: 14px;
  font-weight: 500;
  color: #303133;
  margin: 0 0 15px 0;
}

.add-service {
  text-align: center;
  padding: 20px 0;
  border-top: 1px solid #EBEEF5;
}
</style>